package com.arunsawad.ilertu.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.arunsawad.ilertu.R;
import com.arunsawad.ilertu.adapter.EmergenciesAdapter;
import com.arunsawad.ilertu.common.Constants;
import com.arunsawad.ilertu.common.RightDrawableOnTouchListener;
import com.arunsawad.ilertu.dao.EmergencyDao;
import com.arunsawad.ilertu.entity.Emergency;

public class Emergencies extends Activity {
	SharedPreferences preferences;
	private EmergencyDao emergencyDao;
	private EmergenciesAdapter adapter;
	private List<Emergency> emergencies = new ArrayList<Emergency>();
	EditText txtSearch;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.act_emergencies);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.title0_imgbtn_txt);
		
		initTitle();

		preferences = getSharedPreferences(Constants.PREF_FILE, MODE_PRIVATE);
		emergencyDao = new EmergencyDao(this);
		txtSearch = (EditText) findViewById(R.id.emergencies_txtSearch);

		emergencies = emergencyDao.getEmergencies("",
				preferences.getString(Constants.PREF_LANGUAGE, "en"));

		if (emergencies.size() == 0) {
			emergencies = addEmergencies();
		}

		final Drawable x = getResources().getDrawable(R.drawable.delete);
		txtSearch.addTextChangedListener(new TextWatcher() {
			public void afterTextChanged(Editable s) {
				if (txtSearch.getText().toString().equals("")) {
					txtSearch.setCompoundDrawables(null, null, null, null);
				} else {
					x.setBounds(0, 0, x.getIntrinsicWidth(),
							x.getIntrinsicHeight());
					txtSearch.setCompoundDrawables(null, null, x, null);
				}
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
			}
		});

		txtSearch.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// If the event is a key-down event on the "enter" button
				if ((event.getAction() == KeyEvent.ACTION_DOWN)
						&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
					// Perform action on key press
					emergencies = emergencyDao.getEmergencies(txtSearch
							.getText().toString(), preferences.getString(
							Constants.PREF_LANGUAGE, "en"));
					render();
					return true;
				}
				return false;
			}
		});

		txtSearch.setOnTouchListener(new RightDrawableOnTouchListener(
				txtSearch, x) {
			public boolean onDrawableTouch(final MotionEvent event) {
				txtSearch.setText("");
				txtSearch.setCompoundDrawables(null, null, null, null);

				emergencies = emergencyDao.getEmergencies("",
						preferences.getString(Constants.PREF_LANGUAGE, "en"));
				render();
				return false;
			}
		});

		render();
	}
	
	public void initTitle() {
		ImageButton imgBtn1 = (ImageButton) findViewById(R.id.title0_imgBtn1);
		imgBtn1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(), Home.class);
				startActivity(intent);
			}
		});

		TextView txtTitle = (TextView) findViewById(R.id.title0_txtTitle);
		txtTitle.setText(getString(R.string.emergencies));
	}

	public void render() {
		// create base adapter
		adapter = new EmergenciesAdapter(Emergencies.this, emergencies);
		ListView listView = (ListView) findViewById(R.id.lv_emergencies);
		listView.setAdapter(adapter);

		for (Emergency emergency : emergencies) {
			emergency.loadImage(adapter, this, getPackageName());
		}

		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(Intent.ACTION_CALL);
				intent.setData(Uri.parse("tel:"
						+ emergencies.get(position).getTel()));
				startActivity(intent);
			}
		});
	}

	public List<Emergency> addEmergencies() {
		List<Emergency> emergencies = new ArrayList<Emergency>();

		Emergency emergency;
		emergency = new Emergency("Flood Relief Operation Center (FROC)",
				"ศปภ.", "http://www.floodthailand.net/", "e001", "1111");
		emergencies.add(emergency);
		emergency = new Emergency("Bangkok matropolitan Administration",
				"สายด่วน กทม. ", "http://www.bangkok.go.th", "e002", "1555");
		emergencies.add(emergency);
		emergency = new Emergency("National Disater Warning Center",
				"ศูนย์เตือนภัยภิบัติ", "http://www.ndwc.go.th", "e003", "192");
		emergencies.add(emergency);
		emergency = new Emergency(
				"Department of Disaster Prevention and Mitigation",
				"สายด่วน ปภ. (กรมป้องกันและบรรเทาสาธารณภัย)  ",
				"http://www.disaster.go.th/dpm/", "e004", "1784");
		emergencies.add(emergency);
		emergency = new Emergency("FM 91.0 MHZ", "สวพ.91", "www.fm91bkk.com",
				"e005", "1644");
		emergencies.add(emergency);
		emergency = new Emergency("js100radio", "จส.100 ", "www.js100.com",
				"e006", "1137");
		emergencies.add(emergency);
		emergency = new Emergency("Thai Traffic Police", "ตร.จราจร ",
				"http://www.trafficpolice.go.th", "e007", "1197");
		emergencies.add(emergency);
		emergency = new Emergency("Department of Highways", "กรมทางหลวง",
				"http://www.doh.go.th/", "e008", "1586");
		emergencies.add(emergency);
		emergency = new Emergency("Bangkok Mass Transit Authority", "ขสมก. ",
				"http://www.bmta.co.th", "e009", "1348");
		emergencies.add(emergency);
		emergency = new Emergency("Exat Call Center",
				"การทางพิเศษแห่งประเทศไทย", "http://www.exat.co.th/", "e010",
				"1543");
		emergencies.add(emergency);
		emergency = new Emergency("State Railway of Thailand Information",
				"รถไฟแห่งประเทศไทย", "www.railway.co.th/", "e011", "1690");
		emergencies.add(emergency);
		emergency = new Emergency("Royal Irrigation Department",
				"สอบถามข้อมูล สถานการณ์น้ำ (สายด่วนกรมชลประทาน)",
				"http://www.rid.go.th", "e012", "1460");
		emergencies.add(emergency);
		emergency = new Emergency("Highway Police", "เช็คเส้นทางตร.ทางหลวง",
				"http://www.highwaypolice.org/", "e013", "1193");
		emergencies.add(emergency);
		emergency = new Emergency("The Transport Co., Ltd.",
				"บขส. (สอบถามเส้นทางเดินรถต่างจังหวัด)",
				"http://www.transport.co.th/", "e014", "1490");
		emergencies.add(emergency);
		emergency = new Emergency("Emergency Medical Institute Of Thailand",
				"ศูนย์การแพทย์ฉุกเฉินแห่งชาติ (สพฉ)", "http://www.emit.go.th",
				"e015", "1669");
		emergencies.add(emergency);
		emergency = new Emergency("Bangkok EMS",
				"ศูนย์บริการการแพทย์ฉุกเฉิน กรุงเทพมหานคร (ศูนย์เอราวัณ)",
				"http://www.ems.bangkok.go.th/", "e016", "1646");
		emergencies.add(emergency);
		emergency = new Emergency("Ministry of Labour", "สายด่วนกระทรวงแรงงาน",
				"http://www.mol.go.th", "e017", "1506");
		emergencies.add(emergency);
		emergency = new Emergency("Metropolitan Electricity Authority",
				"ไฟฟ้านครหลวง ", "http://www.mea.or.th", "e018", "1130");
		emergencies.add(emergency);
		emergency = new Emergency("Provincial Electricity Authority",
				"ไฟฟ้าส่วนภูมิภาค ", "http://www.pea.co.th", "e019", "1129");
		emergencies.add(emergency);
		emergency = new Emergency("Metropolitan Waterworks Authority ",
				"การประปานครหลวง ", "http://www.mwa.co.th", "e020", "1125");
		emergencies.add(emergency);
		emergency = new Emergency("Department of Mental Health",
				"สายด่วนสุขภาพจิต กรมสุขภาพจิต", "http://www.dmh.go.th/",
				"e021", "1323");
		emergencies.add(emergency);
		emergency = new Emergency("Ministry of Education",
				"ศูนย์พักพิงผู้ประสบอุทกภัย กระทรวงศึกษาธิการ",
				"http://www.moe.go.th", "e022", "1579");
		emergencies.add(emergency);
		emergency = new Emergency("Krob Krua Kao", "ข่าวช่อง 3 ",
				"http://www.krobkruakao.com/", "e023", "022623331");
		emergencies.add(emergency);
		emergency = new Emergency(
				"Thai Public Broadcasting Service (Thai PBS)", "ThaiPBS ",
				"http://www.thaipbs.or.th/", "e024", "027902111");
		emergencies.add(emergency);
		emergency = new Emergency("Provincial Waterworks Authority",
				"การประปาส่วนภูมิภาค ", "ww.pwa.co.th/", "e025", "1662");
		emergencies.add(emergency);
		emergency = new Emergency("Bangkok Fire and Rescue Department",
				"ศูนย์ดับเพลิง สัตว์เข้าบ้าน ", "http://www.bangkokfire.com",
				"e026", "199");
		emergencies.add(emergency);
		emergency = new Emergency("Marine Department of Thailand ",
				"กรมเจ้าท่า (อุบัติเหตุทางน้ำ)", "http://www.md.go.th", "e028",
				"1199");
		emergencies.add(emergency);
		emergency = new Emergency("Emergency Call 191", "เหตุด่วนเหตุร้าย",
				"http://www.patrolcops191.com/", "e029", "191");
		emergencies.add(emergency);
		emergency = new Emergency("Chemical Emergency Response Center",
				"แจ้งเหตุฉุกเฉินอุบัติภัยสารเคมี", "http://www.pcd.go.th",
				"e030", "1650");
		emergencies.add(emergency);
		emergency = new Emergency("Food and Drug Administration",
				"สายด่วนผู้บริโภค อย. ", "http://www.fda.moph.go.th/", "e031",
				"1556");
		emergencies.add(emergency);
		emergency = new Emergency("Crime Suppression Division Thai Police",
				"กองปราบปราม แจ้งข่าวอาชญากรรม", "http://www.csd.go.th",
				"e032", "1195");
		emergencies.add(emergency);
		emergency = new Emergency("Tourist Police Division", "ตำรวจท่องเที่ยว",
				"http://www.thailandtouristpolice.com/", "e033", "1155");
		emergencies.add(emergency);
		emergency = new Emergency("Ruamkatanyu Foundation",
				"มูลนิธิร่วมกตัญญู", "http://www.ruamkatanyu.or.th", "e037",
				"027510951");
		emergencies.add(emergency);
		emergency = new Emergency("Traffic Radio Society FM 99.5",
				"สถานีวิทยุจราจรเพื่อสังคม FM 99.5", "http://www.trs995.com",
				"e038", "1255");
		emergencies.add(emergency);
		emergencyDao.addEmergencies(emergencies);

		return emergencies;
	}
}
